// pages/addPatient/addPatient.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        patientName: '',
        identityNumber: '',
        sex: '',
        sexInit: '',
        birth: '',
        birthInit: '',
        telephone: '',
        checked: false,
        sexColumns: [
            "男","女"
        ],
        isSex: false,
        isBirth: false,
        isBtn: true,
        currDate: new Date().getTime(),
        minDate: new Date(1900,1,1).getTime(),
        formatter(type, value) {
          if (type === 'year') {
            return `${value}年`;
          }
          if (type === 'month') {
            return `${value}月`;
          }
          return value;
        },
    },

    selectSex() {
        this.setData({
            isBirth : false,
            isSex : true,
            isBtn: false
        })
    },

    selectBirth() {
        this.setData({
            isSex : false,
            isBirth : true,
            isBtn: false
        })
    },

    onSexConfirm(data) {
        this.setData({
            isSex : false,
            sex: data.detail.value,
            sexInit: data.detail.index,
            isBtn: true
        })
    },

    onSexCancel() {
        this.setData({
            isSex : false,
            isBtn: true
        })
    },

    onBirthConfirm(date) {
        // console.log(date)
        // console.log(date.detail)
        let ymd  = new Date(date.detail)
        // console.log(ymd)
        // console.log(ymd)
        // console.log(111)
        let year = ymd.getFullYear()
        let month = ymd.getMonth() + 1
        let day = ymd.getDate()
        // console.log(year)
        // console.log(month)
        // console.log(day)
        let ymdInput = `${year}-${month}-${day}`
        console.log(ymdInput)
        console.log(ymd)
        this.setData({
            birth: ymdInput,
            birthInit: ymd,
            isBirth: false,
            isBtn: true
        })
        // console.log(typeof(birth))
        // console.log(birth.subString(0,9))
        // this.setData({
        //     isSex : false,
        //     sex: data.detail.value,
        //     sexInit: data.detail.index,
        // })
    },

    onBirthCancel() { 
        this.setData({
            isBirth : false,
            isBtn: true
        })
    },

    switchChange(data) {
        
        this.setData({
            checked : data.detail
        })
    },

    savePatient() {
        let userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        // console.log(this.data.patientName)
        // console.log(this.data.sex)
        // console.log(this.data.identityNumber)
        // console.log(this.data.birth)
        // console.log(this.data.telephone)
        // console.log(this.data.checked)
        let isDefault = this.data.checked == false ? 0 : 1
        let params = {
            userId: userInfo.id,
            name: this.data.patientName,
            identityNumber: this.data.identityNumber,
            birthday: this.data.birthInit,
            sex: this.data.sexInit,
            telephone: this.data.telephone,
            acquiesce:  isDefault
        }
        console.log(params)
        wx.request({
          url: 'https://118.31.170.229:5001/api/ZshPatient/Add',
          method: 'post',
          data : params,
          success: (res) => {
              wx.navigateTo({
                url: '../zshPatientList/zshPatientList',
              })
          }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})